package com.example.c_reflect;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class DemoTestMethods {
    public static void main(String[] args) throws InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        Class<Person> personClass = Person.class;
        Method[] methods = personClass.getMethods();
        for (Method method : methods) {
            System.out.println(method);
        }

        System.out.println("=========================");
        Method setName = personClass.getMethod("setName", String.class);
        Person person = personClass.newInstance();
        setName.invoke(person, "刘思思");
        System.out.println("person:" + person);

        System.out.println("=========================");
        Method[] declaredMethods = personClass.getDeclaredMethods();
        for (Method declaredMethod : declaredMethods) {
            System.out.println("declaredMethod: " + declaredMethod);
        }

        Method method = personClass.getDeclaredMethod("eat");
        method.setAccessible(true);
        method.invoke(person);
    }
}
